$(function () {

    //变量抽取
    var employee_dataGrid = $("#employee_dataGrid");
    var employee_dialog = $("#employee_dialog");
    var employee_form = $("#employee_form");


    employee_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/employee/query.do",
        fit: true,
        align: 'center',
        columns: [[
            {field: 'username', title: '用户名', width: 100,align:'center'},
            {field: 'realname', title: '真实名称', width: 100,align:'center'},
            {field: 'tel', title: '电话', width: 100,align:'center'},
            {field: 'email', title: '邮箱', width: 100,align:'center'},
            {
                field: 'dept', title: '部门', width: 100,align:'center', formatter: function (value, row, index) {
                return value ? value.name : "";
            }
            },
            {field: 'hireDate', title: '入职时间', width: 100,align:'center', sortable: true},
            {
                field: 'state', title: '状态', width: 100,align:'center', formatter: function (value, row, index) {
                return value ? "在职" : "离职";
            }
            },
            {
                field: 'admin', title: '管理员', width: 100,align:'center', formatter: function (value, row, index) {
                return value ? "管理员" : "普通用户";
            }
            }
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
        sortName: 'hireDate',
        sortOrder: 'DESC',
        onSelect: function (index, row) {
            if (row.state) {
                $("#state_btn").linkbutton({
                    text: "离职"
                })
            } else {
                $("#state_btn").linkbutton({
                    text: "复职"
                })
            }
        }
    });

    employee_dialog.dialog({
        width: 280,
        height: 340,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            employee_form.form("clear");
        },
        resizeable:true
    })

    //方法抽成对象统一管理
    var methodObj = {
        searchForm: function () {
            var keyword = $("[name=keyword]").val();
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            var deptId = $(".dept_combobox").combobox('getValue');

            employee_dataGrid.datagrid('load', {
                keyword: keyword,
                beginTime: beginTime,
                endTime: endTime,
                deptId: deptId
            });
        },
        initPassword: function () {
            var row = employee_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中一条数据', 'info');
                return;
            }
            $.messager.confirm('提示', '确认重置该用户密码么?', function (r) {
                if (r) {
                    $.get("/employee/initPassword.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('提示', '操作成功', 'info', function () {
                                employee_dataGrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('提示', data.msg, 'error')
                        }
                    })
                }
            })
        },
        add: function () {
            employee_dialog.dialog("setTitle", "新增");
            employee_dialog.dialog("open");
            $("#password_tr").show();

        },
        edit: function () {
            var row = employee_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中编辑数据', 'info');
                return;
            }
            $("#password_tr").hide();
            $.get("/role/getRoleIdByEmployeeId.do", {employeeId: row.id}, function (data) {
                $("#relo_combobox").combobox("setValues",data);
            })

            //处理部门数据
            row["dept.id"] = row.dept.id;
            employee_form.form("load", row);
            employee_dialog.dialog("setTitle", "编辑")
            employee_dialog.dialog("open");
        },
        changState: function () {
            var row = employee_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中一条数据', 'info');
                return;
            }
            $.messager.confirm('提示', '确认更改该数据状态么?', function (r) {
                if (r) {
                    $.get("/employee/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('提示', '操作成功', 'info', function () {
                                employee_dataGrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('提示', data.msg, 'error')
                        }
                    })
                }
            })

        },
        reload: function () {
            employee_dataGrid.datagrid("load");
        },
        save: function () {
            employee_form.form('submit', {
                url: "/employee/saveOrUpdate.do",
                onSubmit: function (param) {
                    var ids = $("#relo_combobox").combobox("getValues");
                    for (var i = 0; i < ids.length; i++) {
                        param["roles[" + i + "].id"] = ids[i];
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            employee_dataGrid.datagrid("load");
                        })
                    } else {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                }
            })
        },
        cancel: function () {
            employee_dialog.dialog("close");
        }
    }

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
})
